﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace CommLib.BdPush
{
    /// <summary>
    /// 作者：冼树华
    /// QQ：56472465
    /// 日期：20150-04-29
    /// 功能：提交发送信息到百度接口的抽象接口
    /// </summary>
    public abstract class Baidu_Send
    {
        public Baidu_Mod mod { get; set; }
        public string url { get; set; }
        public string httpMehtod { get; set; }
        public string secret_key { get; set; }

        public string PushMessage()
        {
            string strResult = "";

            //1.创建异步任务
            Task<string> task = Baidu_Helper.SendBaidu(this.httpMehtod, this.url, this.secret_key, this.mod);
            if (null != task)
            {
                //2.等待任务完成
                task.Wait();

                //3.异步任务完成
                if (task.IsCompleted)
                {
                    strResult = task.Result.ToString();
                }
            }

            return strResult;
        }
    }

    /// <summary>
    /// 作者：闲人工作室 Freeman
    /// QQ：15832782
    /// 日期：2016-05-03
    /// </summary>
    public class Baidu_Send<T> : Baidu_Send
    {
        public BdResponse<T> PushMessageAndResponse()
        {
            string strResult = PushMessage();
            try
            {
                var result = (BdResponse<T>)JsonConvert.DeserializeObject(strResult, typeof(BdResponse<T>));
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}